<?php

/**
 * Tabela do texto inicial.
 *
 * @author Fernando Dias Oliveira <rodox17@gmail.com>
 * @package lore
 * @subpackage model
 */

class caf04 extends Zend_Db_Table_Abstract  
{
	protected $_table   = "caf04";
	protected $_primary = "caf04_cod_menu";
	
	public function getProductsByMenu($id_menu)
	{
		$arr = array();
		
		foreach ($this->fetchAll("caf04_cod_menu = $id_menu")->toArray() as $val)
		{
			$arr[] = array("caf04_cod_prod" => $val["caf04_cod_prod"]);
		}
		
		return $arr;
	}
	
	public function getItensById($cod, $cod_item)
	{
		$db = $this->getAdapter();
		$arr = array();
		
		//$arrTable = $db->fetchAll("SELECT distinct caf04.caf04_cod_prod, caf05_prod, caf05_car, caf03_menu, caf05_cod, caf05_valor FROM caf05, caf03, caf04 WHERE caf04_cod_prod = caf05_cod AND caf05_cod_menu = caf03_cod AND caf05_cod_menu = $cod_menu ORDER BY caf05_prod ASC");
		$arrTable = $db->fetchAll("SELECT * FROM caf05, caf04
									 WHERE caf04_cod_item = $cod_item AND 
								
									 caf04_cod_prod = caf05_cod
									 ORDER BY caf05_prod ASC");
		
		return $arrTable;
	}
	
	public function getAll()
	{
		$db = $this->getAdapter();
		
		return $db->fetchAll("SELECT * FROM caf04
									left join caf05 on(caf05_cod = caf04_cod_prod)
									left join caf03 on(caf03_cod = caf05_cod_menu)
									left join caf02 on(caf02_cod = caf04_cod_item)
									left join caf01 on(caf01_cod = caf02_cod_user)
									WHERE 
									caf05_cod_menu = caf03_cod AND
									caf02_active = 1
									ORDER BY caf02_mesa ASC");
	}
	
	public function getAllOrdens()
	{
		$db = $this->getAdapter();
		
		return $db->fetchAll("SELECT * FROM caf04
									left join caf05 on(caf05_cod = caf04_cod_prod)
									left join caf03 on(caf03_cod = caf05_cod_menu)
									left join caf02 on(caf02_cod = caf04_cod_item)
									left join caf01 on(caf01_cod = caf02_cod_user)
									WHERE 
									caf05_cod_menu = caf03_cod AND
									caf02_active = 1
									ORDER BY caf05_cod ASC");
	}
	
	public function getTotalByUser($data, $inicio, $fim)
	{
		$db = $this->getAdapter();
		
		if($inicio)
		{
			return $db->fetchAll("SELECT SUM( caf05_valor ) AS caf05_valor, caf01_user
								FROM caf04
								LEFT JOIN caf05 ON ( caf05_cod = caf04_cod_prod )
								LEFT JOIN caf03 ON ( caf03_cod = caf05_cod_menu )
								LEFT JOIN caf02 ON ( caf02_cod = caf04_cod_item )
								LEFT JOIN caf01 ON ( caf01_cod = caf02_cod_user )
								WHERE caf05_cod_menu = caf03_cod
								AND DATE_FORMAT(caf02_date, '%d/%m/%Y') >= '$inicio'
								AND DATE_FORMAT(caf02_date, '%d/%m/%Y') <= '$fim'
								GROUP BY caf01_user
								ORDER BY caf02_mesa ASC");
		}
		
		return $db->fetchAll("SELECT SUM( caf05_valor ) AS caf05_valor, caf01_user
								FROM caf04
								LEFT JOIN caf05 ON ( caf05_cod = caf04_cod_prod )
								LEFT JOIN caf03 ON ( caf03_cod = caf05_cod_menu )
								LEFT JOIN caf02 ON ( caf02_cod = caf04_cod_item )
								LEFT JOIN caf01 ON ( caf01_cod = caf02_cod_user )
								WHERE caf05_cod_menu = caf03_cod
								AND DATE_FORMAT(caf02_date, '%d/%m/%Y') = '$data'
								GROUP BY caf01_user
								ORDER BY caf02_mesa ASC");
	}
	
	public function getTotalPorMesa($numeroMesa)
	{
		$sql = "SELECT SUM(caf05_valor) as valor FROM caf04
									left join caf05 on(caf05_cod = caf04_cod_prod)
									left join caf03 on(caf03_cod = caf05_cod_menu)
									left join caf02 on(caf02_cod = caf04_cod_item)
									left join caf01 on(caf01_cod = caf02_cod_user)
									WHERE 
									caf05_cod_menu = caf03_cod AND
									caf02_mesa = $numeroMesa AND
									caf02_active = 1";
								
		return $this->getAdapter()->fetchOne($sql);
	}
	
	public function getTotalCaixa($data, $inicio, $fim)
	{
		$db = $this->getAdapter();
		
		if($inicio)
		{
			return $db->fetchAll("SELECT SUM( caf05_valor ) AS caf05_valor, caf02_date
								FROM caf04
								LEFT JOIN caf05 ON ( caf05_cod = caf04_cod_prod )
								LEFT JOIN caf03 ON ( caf03_cod = caf05_cod_menu )
								LEFT JOIN caf02 ON ( caf02_cod = caf04_cod_item )
								LEFT JOIN caf01 ON ( caf01_cod = caf02_cod_user )
								WHERE caf05_cod_menu = caf03_cod
								AND DATE_FORMAT(caf02_date, '%d/%m/%Y') >= '$inicio'
								AND DATE_FORMAT(caf02_date, '%d/%m/%Y') <= '$fim'
								ORDER BY caf02_mesa ASC");
		}
		
		return $db->fetchAll("SELECT SUM( caf05_valor ) AS caf05_valor, caf02_date
								FROM caf04
								LEFT JOIN caf05 ON ( caf05_cod = caf04_cod_prod )
								LEFT JOIN caf03 ON ( caf03_cod = caf05_cod_menu )
								LEFT JOIN caf02 ON ( caf02_cod = caf04_cod_item )
								LEFT JOIN caf01 ON ( caf01_cod = caf02_cod_user )
								WHERE caf05_cod_menu = caf03_cod
								AND DATE_FORMAT(caf02_date, '%d/%m/%Y') = '$data'
								ORDER BY caf02_mesa ASC");
	}
	
	public function save($arrPost)
	{
		die($arrPost["caf04_qtd"] + "bunda");
		for($i = 0; $i < $arrPost["caf04_qtd"]; $i++)
		{
			$this->insert($arrPost);
		}
	}
	
	public function saveComent($post)
	{
		$this->update(array("caf04_coment" => $post["caf04_coment"], "caf04_cadeira" => $post["caf04_cadeira"]), "caf04_cod = {$post["caf04_cod"]}");
	}
	
	public function saveComentAll($post)
	{
		$this->update(array("caf04_coment" => $post["caf04_coment"]), "caf04_cod_prod = {$post["caf04_cod_prod"]} AND caf04_cod_item = {$post["caf04_cod_item"]}");
	}
	
	public function saveDesconto($post)
	{
		$this->update(array("caf04_desconto" => $post["desconto"]), "caf04_cod = {$post["caf04_cod"]}");
	}
	
	public function getProductsByCodPedido($cod_pedido)
	{
		$db = $this->getAdapter();
		
		return $db->fetchAll("SELECT * FROM caf03, caf04, caf05 WHERE caf04_cod_item = $cod_pedido AND caf05_cod = caf04_cod_prod AND caf05_cod_menu = caf03_cod  ORDER BY caf05_cod_menu, caf05_prod, caf05_car, caf04_coment ASC");
	}
	
	public function getProductsByCodMenu($cod, $cod_menu)
	{
		$db = $this->getAdapter();
		
		return $db->fetchAll("SELECT * FROM caf03, caf04, caf05 WHERE caf04_cod_item = $cod AND caf05_cod = caf04_cod_prod AND caf05_cod_menu = caf03_cod AND caf05_cod_menu = '$cod_menu'  ORDER BY caf05_cod_menu, caf05_prod, caf05_car ASC");
	}
	
	public function saveUn($cod_menu, $cod_item, $cod_prod, $qtd)
	{
		$db = $this->getAdapter();
		
			$db->query("INSERT INTO caf04(caf04_cod_menu, caf04_cod_prod, caf04_qtd, caf04_cod_item) VALUES('$cod_menu', '$cod_prod', 1, $cod_item)");
		/*$strWhere = "caf04_cod_menu = $cod_menu AND caf04_cod_prod = $cod_prod AND caf04_cod_item = $cod_item";
		
		if($this->fetchAll($strWhere)->toArray())
		{
			$this->update(array("caf04_qtd" => $qtd), $strWhere);
			
			return;
		}
		
		return $this->insert(array("caf04_cod_menu" => $cod_menu,"caf04_cod_item" => $cod_item, "caf04_cod_prod" => $cod_prod, "caf04_qtd" => $qtd));		
*/	
}
	
	public function deleteUn($cod_menu, $cod_item, $cod_prod, $qtd)
	{
		$arr = $this->fetchAll("caf04_cod_prod = $cod_prod AND caf04_cod_item = $cod_item")->current()->toArray();
		
		//print_r($arr);
		
		$strWhere = "caf04_cod = {$arr["caf04_cod"]}";
		
		$this->delete($strWhere);
	}
	
	public function setEntregue($cod)
	{
		$date = date("Y-m-d h:i:s");
		
		$this->update(array("caf04_entregue" => $date), "caf04_cod = $cod");
	}
}

?>
